package com.augurit.wf.common.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.wf.client.agency.web.form.WfAgencyForm;
import com.augurit.wf.common.entity.Jbpm4HistTask;
import com.augurit.wf.common.entity.Jbpm4Task;

@Repository
public class Jbpm4HistTaskDao extends BaseDao<Jbpm4HistTask, Long> {
	
	//获取上一环节的任务列表
	public List<Jbpm4HistTask> getPrivHistTasksById(Long prevTaskDbId){
		List<Jbpm4HistTask> list = null;
		if(prevTaskDbId != null){
			String hql = "from Jbpm4HistTask ps where (ps.supertask = " + prevTaskDbId
			+ " or ps.histTaskInstDbid  =" + prevTaskDbId
			+ " ) order by ps.isMainTask desc, ps.create";

			list = this.find(hql);
		}
		return list;
	}
	
	public void clearAgent(Jbpm4HistTask task){
		if(task != null){
			task.setAgent(null);
			task.setAgentName(null);
			task.setAgentStartDate(null);
			task.setAgentEndDate(null);
			this.save(task);
		}
	}
	
	public void clearAgent(Long taskId){
		if(taskId != null){
			Jbpm4HistTask task = this.get(taskId);
			clearAgent(task);
		}
	}
	
	public void setAgent(Jbpm4HistTask task, WfAgencyForm form){
		if(task != null && form != null){
			task.setAgent(form.getAgent());
			task.setAgentName(form.getAgentName());
			task.setAgentStartDate(form.getStartDate());
			task.setAgentEndDate(form.getEndDate());
			this.save(task);
		}
	}
}